/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.andycole.mdrmodel;

import java.io.File;
import javax.jmi.reflect.RefPackage;
import org.netbeans.api.mdr.MDRepository;
import org.netbeans.api.mdr.MDRManager;
import org.netbeans.api.mdr.JMIMapper;
import org.netbeans.lib.jmi.mapping.FileStreamFactory;

/**
 *
 * @author andy
 */
public class JmiInterfaceGenerator {

   //private String destinationDir = "generated";
   //private String extentName = "UMLMM";

   public static void generate(String extentName, String destinationDir)
   {
      MDRepository repository = MDRManager.getDefault().getDefaultRepository();
      RefPackage extent = repository.getExtent(extentName);
      if (extent == null)
      {
         System.err.println("Could not find extent "+extentName);
         return;
      }

      try
      {
         repository.beginTrans(false);

         String dirName = destinationDir;
         File file = new File(dirName);
         boolean directoryMade = file.mkdir();
         if (directoryMade)
            System.out.println("Directory created at "+dirName);
         
         JMIMapper mapper = JMIMapper.getDefault();
         //RefPackage tmp = uml.refOutermostPackage();
         //RefObject meta = uml.refMetaObject();
         //Object bb = meta.refMetaObject();
         RefPackage metaPkg = extent.refMetaObject().refImmediatePackage();

         mapper.generate(new FileStreamFactory(file), metaPkg);
      }
      catch(Exception e)
      {
         System.err.println("Could not generate JMI sources: "+e);
      }
      finally
      {
         repository.endTrans();
      }
   }
}
